#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include "SDLEngine.h"
#include "../Model/AppList.h"
#include <SDL/SDL.h>

SDL_Surface* _screen;
bool _quit = false;
SDL_Event _event;

void SDLEngine::init(GraphicEngine* ge, AppList* list, 
		     int* argc, char ** argv) {

  mask = false;
  local_ge = ge;
  state = IN_APP;
  _list = list;

}

void SDLEngine::refresh() {
  SDL_GL_SwapBuffers();
}

void SDLEngine::start() {
  SDL_Init(SDL_INIT_VIDEO);
  
  _screen = SDL_SetVideoMode(local_ge->getXRes(),
			     local_ge->getYRes(),
			     32,SDL_OPENGL);
  
  SDL_Flip(_screen);
  
  while (!_quit)
    {
      while(SDL_PollEvent(&_event)){
	switch(_event.type)
	  {
	  case SDL_KEYDOWN:
	    switch(_event.key.keysym.sym){
	    case SDLK_LEFT:
	      local_ge->rotateLeft();
	      SDLEngine::refresh();
	      _list->swapNext();
	      break;
	    case SDLK_RIGHT:
	      local_ge->rotateRight();
	      SDLEngine::refresh();
	      _list->swapPrec();
	      break;
	    }
	    break;
	    
	  case SDL_QUIT:
	    _quit = true;
	  }
      }

      local_ge->commonGL();
      switch (state) {
      case IN_APP:
	local_ge->draw();
	break;
      case ROTATING_RIGHT:
	local_ge->drawRotateRight();
	break;
      case ROTATING_LEFT:
	local_ge->drawRotateLeft();
	break;
      }
      local_ge->drawDock();

      SDLEngine::refresh();
	
    }
  SDL_Quit();
}

int SDLEngine::getTime() {
  return glutGet(GLUT_ELAPSED_TIME);
}

void SDLEngine::drawText(std::string text) {

}


AppList* GlutEngine::_list;
GraphicEngine* GlutEngine::local_ge;
DrawState GlutEngine::state;
bool GlutEngine::mask;
